package io.knifer.gokexceltask.excel.validator;

import com.google.common.base.Joiner;
import io.knifer.gokexceltask.dao.ClasseDAO;
import io.knifer.gokexceltask.dao.FacultiesDAO;
import io.knifer.gokexceltask.dao.MajorDAO;
import io.knifer.gokexceltask.dao.StudentDAO;
import io.knifer.gokexceltask.excel.converter.IdCardNumberEncryptConverter;
import io.knifer.gokexceltask.exception.InvalidStudentDataException;
import io.knifer.gokexceltask.pojo.domain.Student;
import io.knifer.gokexceltask.utils.AESUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.security.InvalidKeyException;
import java.util.ArrayList;
import java.util.List;

@Component
public class StudentDataValidator implements DataValidator<Student>{
    private final StudentDAO studentDAO;
    private final MajorDAO majorDAO;
    private final FacultiesDAO facultiesDAO;
    private final ClasseDAO classeDAO;
    private final Joiner joiner;

    public StudentDataValidator(StudentDAO studentDAO, MajorDAO majorDAO, FacultiesDAO facultiesDAO, ClasseDAO classeDAO, Joiner joiner){
        this.studentDAO = studentDAO;
        this.majorDAO = majorDAO;
        this.facultiesDAO = facultiesDAO;
        this.classeDAO = classeDAO;
        this.joiner = joiner;
    }

    public void validate(Student student, boolean miscDataAutoInsert) throws InvalidStudentDataException {
        List<String> msg = new ArrayList<>();
        Student.Status status = student.getStatus();
        String major = student.getMajor().getName();
        String classe = student.getClasse().getName();
        String faculties = student.getFaculties().getName();

        if (StringUtils.isBlank(major)){
            msg.add("专业为空");
        }
        if (StringUtils.isBlank(classe)){
            msg.add("班级为空");
        }
        if (StringUtils.isBlank(faculties)){
            msg.add("学院为空");
        }
        if (StringUtils.isBlank(student.getStudentId())){
            msg.add("学号为空");
        }
        if (StringUtils.isBlank(student.getIdCardNumber())){
            msg.add("身份证号为空");
        }
        if (status == Student.Status.UNKNOWN){
            msg.add("学生状态不合法");
        }
        if (studentDAO.existsByIdCardNumber(student.getIdCardNumber())){
            msg.add("【" + getDecryptedIdCardNumber(student) + "】身份证号已存在");
        }
        if (studentDAO.existsByStudentId(student.getStudentId())){
            msg.add("【" + student.getStudentId() + "】学号已存在");
        }
        if (!miscDataAutoInsert){
            if (!majorDAO.existsByName(major)){
                msg.add("【" + major + "】专业不存在");
            }
            if (!classeDAO.existsByName(classe)){
                msg.add("【" + classe + "】班级不存在");
            }
            if (!facultiesDAO.existsByName(faculties)){
                msg.add("【" + faculties + "】学院不存在");
            }
        }

        if (!msg.isEmpty()){
            throw new InvalidStudentDataException(msg);
        }
    }

    private String getDecryptedIdCardNumber(Student student){
        String result = null;

        try {
            result = AESUtils.decryptBase64ToPlainText(student.getIdCardNumber(), IdCardNumberEncryptConverter.KEY);
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        }

        return result;
    }
}
